﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineShop.Models;
using OnlineShop.Common;
using OnlineShop.Models.ViewModels;

namespace OnlineShop.Controllers
{
    public class CartController : BaseController
    {
        #region Repository
        private IOnlineShopRepository repository;

        public CartController():this(new EFOnlineShopRepository())
        {
        }

        public CartController(EFOnlineShopRepository eFOnlineShopRepository)
        {
            this.repository = eFOnlineShopRepository;
        }

        #endregion

        //
        // GET: /Cart/

        public ActionResult Index()
        {
            return View(Cart);
        }

        [HttpPost]
        public ActionResult Update(FormCollection frmCollection)
        {
            string values = frmCollection["quantity"];
            Cart.UpdateQuantity(values);
            return RedirectToAction("Index");
        }

        public ActionResult Add(int id)
        {
            var product = repository.GetDetailProduct(id);
            if (product == null)
            {
                return RedirectToHome();
            }

            Cart.AddProduct(product,1);
            Session["Cart"] = Cart;

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            if (id <= 0)
                return RedirectToHome();

            Cart.RemoveProduct(id);
            Session["Cart"] = Cart;

            return RedirectToAction("Index");
        }

        [ChildActionOnly]
        public ActionResult ShowCart()
        {
            return PartialView("ShoppingCart", Cart);
        }
    }
}
